layui.use('table', function () {
    let table = layui.table;
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        var contents = "";
        var dataA = "";
        var countNum = 0;
        if (obj.event === 'detail') {
            /**
             * 查看权限信息
             */
            let id = JSON.stringify(data.id1);
            id = id.replace(/[^0-9]/ig, "");
            console.log("id=" + id);
            $.getJSON("/zwqx/showqx2", "jb_id=" + id, function (aaa) {
                $(aaa[1]).each(function (i, v) {
                    if (v.path !== '') {
                        console.log("-----" + v.path + "----");
                        dataA = dataA + '<tr><td style="padding-left: 10px;"> <input type="checkbox" lay-skin="1" checked value="' + v.id + '" onclick="return false;"> ' + v.auth + '</td></tr>';
                        countNum = countNum + 1;
                    }
                })
                //弹出层数据
                contents = ('<center><div>' +
                    '<table style="padding: 20px;width: 200px;text-align: center;width: 250px">' +
                    '<tr>' +
                    '<td style="padding-left: 10px;"> 职位：' + JSON.stringify(data.name1) + ' <b><hr style="border: 2px solid cadetblue"></b></td>' +
                    '</tr>' + dataA +
                    '</table><hr style="border: 2px solid cadetblue"><b>' + countNum +
                    '条权限</b></div></center>');
                // console.log(contents + "------------------")
                //弹出层
                layer.alert(contents, {
                    title: "查看权限",
                    area: ['300px', '400px'],
                    anim: 4,
                    skin: 'layui-layer-molv',
                    btn: 0,
                });
            });
        } else if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            /**
             * 编辑权限信息
             */
            let id = JSON.stringify(data.id1);
            id = id.replace(/[^0-9]/ig, "");//正则取引号中间的数字
            $.ajaxSettings.async = false;
            $.getJSON("/zwqx/showqx2", "jb_id=" + id, function (aaa) {
                $(aaa[0]).each(function (i, v) {
                    //所有复选框
                    dataA = dataA + '<tr><td style="padding-left: 10px;"> <input type="checkbox" lay-skin="1" id="id' + v.id + '" value="' + v.id + '" > ' + v.auth + '</td></tr>';
                })
                contents = ('<center><div>' +
                    '<table style="padding: 20px;width: 200px;text-align: center;width: 250px" class="table666">' +
                    '<tr>' +
                    '<td style="padding-left: 10px;"> 职位：' + JSON.stringify(data.name1) + ' <b><hr style="border: 2px solid cadetblue"></b></td>' +
                    '</tr>' + dataA +
                    '</table></div></center>');
                // console.log(contents + "------------------")

                //弹出层
                layer.alert(contents, {
                    type: 1,
                    title: "修改权限",
                    area: ['300px', '400px'],
                    btn: ['确认修改', '取消'],
                    btnAlign: 'c',
                    anim: 4,
                    skin: 'layui-layer-molv',
                    yes: function (index, layero) {
                        layer.alert("<br><b>再次确认是否修改权限</b>", {
                            type: 1,
                            icon :2,
                            title: "温馨提示",
                            area: ['200px', '150px'],
                            btn: ['确认', '取消'],
                            btnAlign: 'c',
                            yes: function (index, layero1) {
                                let str = "";
                                //获取复选框选中内容
                                for (let i = 1; i <= aaa[0].length; i++) {

                                    if (layero.find("[id='id" + i + "']:checked").val() != undefined) {
                                        str = str + (layero.find("[id='id" + i + "']:checked").val()) + "-";
                                    }
                                }
                                //若全反选，则添加“-”
                                if (str == ""){
                                    str = "-"
                                }
                                $.ajaxSettings.async = false;
                                $.ajax({
                                    url: "/zwqx/updQX?jb_id=" + id + "&data1=" + str,
                                    type: "GET",
                                    success: function (dataO) {
                                        if (dataO) {
                                            layer.alert("<br><b>修改成功</b>", {
                                                type: 1,
                                                icon:1,
                                                title: "温馨提示",
                                                area: ['200px', '150px'],
                                                btn: ['确认'],
                                                btnAlign: 'c',
                                                yes :function (index) {
                                                    layer.closeAll();
                                                }
                                            })
                                        } else {
                                            layer.alert("<br><b>修改失败</b>", {
                                                type: 1,
                                                icon:1,
                                                title: "温馨提示",
                                                area: ['200px', '150px'],
                                                btn: ['确认'],
                                                btnAlign: 'c',
                                                yes :function (index) {
                                                    layer.closeAll();
                                                }
                                            })
                                        }
                                    }
                                });
                                $.ajaxSettings.async = true;
                            }
                        })
                    }
                });
                let trCount = aaa[0].length;
                //2. 循环遍历所有的复选框
                for (let z = 0; z < trCount; z++) {
                    //3. 抓取所有复选框对应的id
                    let allId = aaa[0][z].id
                    // console.log(allId+"////////////////////")
                    //3. 遍历实际的权限
                    for (let y = 0; y < aaa[1].length; y++) {
                        //4. 做匹配
                        if (allId == aaa[1][y].id) {
                            console.log(aaa[1][y].id)
                            console.log(allId + "////////////////////")
                            //5. 匹配上默认选中
                            $(".table666").find("tr").eq(z + 1).find("input[type='checkbox']").attr("checked", true);
                            //6. 匹配上，停止内层循环，直接走外层循环
                            break;
                        }
                    }
                }
            });
            $.ajaxSettings.async = true;
        }
    });
});

/**
 * 修改职位权限
 */


//
// function xiugai(layero, jb_id) {
//     let str = "";
//     for (let i = 1; i <= 22; i++) {
//         //获取复选框选中内容
//         if (layero.find("[id='id" + i + "']:checked").val() != undefined) {
//             str = str + (layero.find("[id='id" + i + "']:checked").val()) + "-";
//         }
//     }
//     $.ajaxSettings.async = false;
//     $.ajax({
//         url: "/zwqx/updQX?jb_id=" + jb_id + "&data1=" + str,
//         type: "GET",
//         success: function (dataO) {
//
//         }
//     });
//     $.ajaxSettings.async = true;
// }


